package com.luo.demo;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

/**
 * @Author:
 * @Description:
 * @Version: 1.0
 */

public class yang {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        Scanner sc = new Scanner(System.in);
        String[] surnameString = {"赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许", "何", "吕", "施", "张", "上官", "欧阳", "夏侯", "公孙"};
        Map<Integer, String> surname = createSurnameMap(surnameString);
        // 获取到姓名集合的最后一个key转为二进制后的长度 动态初始化数组
        char[] userInputBinary = new char[Integer.toString((Integer) surname.keySet().toArray()[surname.size() - 1], 2).length()];
        Arrays.fill(userInputBinary, '0');
        // 实验任务书要求写法
//        IntStream.range(0, getGroupsByKey(surname).size()).mapToObj(i -> i + 1 + ":" + getGroupsByKey(surname).get(i)).forEach(System.out::println);
//        System.out.println("请输入您所想的姓氏所在位置(以-1表示结束):");
//        while (true) {
//            int userInput = sc.nextInt();
//            if (userInput == -1) break;
//            userInputBinary[userInputBinary.length - userInput] = '1';
//        }
        // 问答写法
        AtomicInteger index = new AtomicInteger(userInputBinary.length);
        IntStream.range(0, getGroupsByKey(surname).size()).mapToObj(i -> i + 1 + ":" + getGroupsByKey(surname).get(i)).forEach(item -> {
            System.out.println(item + "\n" + "请问您的姓氏在上述列中吗(y/n):");
            String userInput = sc.nextLine();
            if (userInput.equalsIgnoreCase("y")) userInputBinary[index.get() - 1] = '1';
            index.getAndDecrement();
        });

        System.out.println("您的姓氏为：" + surname.get(convertBinaryToDecimal(userInputBinary)));
    }

    /**
     * 创建姓氏 Map 集合
     *
     * @param surnameChar 姓氏字符数组
     * @return 姓氏Map集合
     */
    public static Map<Integer, String> createSurnameMap(String[] surnameChar) {
        Map<Integer, String> surname = new LinkedHashMap<>();
        for (int i = 0; i < surnameChar.length; i++) {
            surname.put(i + 1, surnameChar[i]);
        }
        return surname;
    }

    /**
     * 根据key值转二进制后分组
     *
     * @param surname <Integer, String> 姓氏Map集合
     * @return List<String> 姓氏二进制表
     */
    public static List<String> getGroupsByKey(Map<Integer, String> surname) {
        List<String> groups = new ArrayList<>();
        for (int i = 1; i <= surname.size(); i++) {
            // Invoke keySet() to get the keys as a set and cast the type to int[]
            // 商值 初始值等于 key - 1, for(int i = 1) 当前循环集合的index位置
            int quotient = surname.keySet().stream().mapToInt(Integer::intValue).toArray()[i - 1];
            // 计数单位 以0作为个位 以此类推 且每次循环都置为0
            int digit = 0;
            while (quotient != 0) {
                if (quotient % 2 == 1) {
                    if (digit >= groups.size()) {
                        groups.add(digit, " " + surname.get(i));
                    } else {
                        groups.set(digit, groups.get(digit) + " " + surname.get(i));
                    }
                }
                // 商 /= 2 单次求余 当商等于1时 再/2将会丢失精度 结果为0 若商等于0 则说明已经循环到最后
                quotient /= 2;
                // 索引自增一位 遍历下一计数单位
                digit++;
            }
        }
        return groups;
    }

    /**
     * 将char[]从二进制转换为十进制
     *
     * @param userInputBinary char[]
     * @return 十进制值
     */
    public static int convertBinaryToDecimal(char[] userInputBinary) {
        int result = 0;
        for (int i = 0; i < userInputBinary.length; i++) {
            result += (userInputBinary[i] - '0') * Math.pow(2, userInputBinary.length - i - 1);
        }
        return result;
    }
}
